package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.CompanyPositionMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.CompanyPosition;
import edu.hubu.talentmanagementsystem.service.ICompanyPositionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class CompanyPositionServiceImpl extends ServiceImpl<CompanyPositionMapper, CompanyPosition> implements ICompanyPositionService {

    private final MapperContext context;

    public CompanyPositionServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getCompanyPositionMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<CompanyPosition> insert(@EncodeParam CompanyPosition record) {
        int insert = context.getCompanyPositionMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getCompanyPositionMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<CompanyPosition> selectById(Integer id) {
        QueryWrapper<CompanyPosition> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<CompanyPosition> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(CompanyPosition::getCompanyPositionId, id);
        return Optional.of(context.getCompanyPositionMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<CompanyPosition> selectAll(String order) {
        QueryWrapper<CompanyPosition> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<CompanyPosition> select = getColumnWrapper(wrapper.lambda());
            return context.getCompanyPositionMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<CompanyPosition> select = getColumnWrapper(wrapper.lambda());
        return context.getCompanyPositionMapper().selectList(select);
    }

    @Override
    public Page<CompanyPosition> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<CompanyPosition> wrapper = new QueryWrapper<>();
        Page<CompanyPosition> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<CompanyPosition> select = getColumnWrapper(wrapper.lambda());
            return context.getCompanyPositionMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<CompanyPosition> select = getColumnWrapper(wrapper.lambda());
        return context.getCompanyPositionMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<CompanyPosition> getColumnWrapper(LambdaQueryWrapper<CompanyPosition> wrapper) {
        return wrapper.select(
                CompanyPosition::getCompany
                    ,
                CompanyPosition::getCompanyPositionId
                    ,
                CompanyPosition::getCompanyPositionNum
                    ,
                CompanyPosition::getCompanyPositionName
                    ,
                CompanyPosition::getCompanyPositionCompanyId
                    ,
                CompanyPosition::getCompanyPositionDepartmentId
        );
    }

    @Override
    public Page<CompanyPosition> selectLikePaged( Integer currentPage,
             Integer pageSize, CompanyPosition record, String order) {
        Page<CompanyPosition> page = new Page<>(currentPage, pageSize);
        QueryWrapper<CompanyPosition> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getCompanyPositionMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getCompanyPositionMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<CompanyPosition> getQueryWrapper(QueryWrapper<CompanyPosition>wrapper, CompanyPosition record) {
        LambdaQueryWrapper<CompanyPosition> lambda = wrapper.lambda();
        if (record.getCompany() != null) {
            lambda = lambda.eq(CompanyPosition::getCompany,record.getCompany());
        }
        if (record.getCompanyPositionId() != null) {
            lambda = lambda.eq(CompanyPosition::getCompanyPositionId,record.getCompanyPositionId());
        }
        if (record.getCompanyPositionCompanyId() != null) {
            lambda = lambda.eq(CompanyPosition::getCompanyPositionCompanyId,record.getCompanyPositionCompanyId());
        }
        if (record.getCompanyPositionDepartmentId() != null) {
            lambda = lambda.eq(CompanyPosition::getCompanyPositionDepartmentId,record.getCompanyPositionDepartmentId());
        }
        if (record.getCompanyPositionNum() != null) {
            lambda = lambda.like(CompanyPosition::getCompanyPositionNum,record.getCompanyPositionNum ());
        }
        if (record.getCompanyPositionName() != null) {
            lambda = lambda.like(CompanyPosition::getCompanyPositionName,record.getCompanyPositionName ());
        }
        return lambda;
    }

    public int count(CompanyPosition record) {
        return context.getCompanyPositionMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<CompanyPosition> updateById(Integer id, @EncodeParam CompanyPosition record) {
        record.setCompanyPositionId(id);
        int update = context.getCompanyPositionMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<CompanyPosition> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<CompanyPosition> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(CompanyPosition::getCompanyPositionId, id);
            return Optional.of(context.getCompanyPositionMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
